package util

import (
	"encoding/json"
	"fmt"
	"reflect"
	"strconv"
	"strings"
	"time"
	"unicode"
)

//字符串处理

// String converts <i> to string.
// It's most common used converting function.
func String(i interface{}) string {
	if i == nil {
		return ""
	}
	switch value := i.(type) {
	case int:
		return strconv.Itoa(value)
	case int8:
		return strconv.Itoa(int(value))
	case int16:
		return strconv.Itoa(int(value))
	case int32:
		return strconv.Itoa(int(value))
	case int64:
		return strconv.FormatInt(value, 10)
	case uint:
		return strconv.FormatUint(uint64(value), 10)
	case uint8:
		return strconv.FormatUint(uint64(value), 10)
	case uint16:
		return strconv.FormatUint(uint64(value), 10)
	case uint32:
		return strconv.FormatUint(uint64(value), 10)
	case uint64:
		return strconv.FormatUint(value, 10)
	case float32:
		return strconv.FormatFloat(float64(value), 'f', -1, 32)
	case float64:
		return strconv.FormatFloat(value, 'f', -1, 64)
	case bool:
		return strconv.FormatBool(value)
	case string:
		return value
	case []byte:
		return string(value)
	case time.Time:
		if value.IsZero() {
			return ""
		}
		return value.String()
	case *time.Time:
		if value == nil {
			return ""
		}
		return value.String()
	default:
		// Empty checks.
		if value == nil {
			return ""
		}
		if f, ok := value.(apiString); ok {
			// If the variable implements the String() interface,
			// then use that interface to perform the conversion
			return f.String()
		} else if f, ok := value.(apiError); ok {
			// If the variable implements the Error() interface,
			// then use that interface to perform the conversion
			return f.Error()
		} else {
			// Reflect checks.
			rv := reflect.ValueOf(value)
			kind := rv.Kind()
			switch kind {
			case reflect.Chan,
				reflect.Map,
				reflect.Slice,
				reflect.Func,
				reflect.Ptr,
				reflect.Interface,
				reflect.UnsafePointer:
				if rv.IsNil() {
					return ""
				}
			}
			if kind == reflect.Ptr {
				return String(rv.Elem().Interface())
			}
			// Finally we use json.Marshal to convert.
			if jsonContent, err := json.Marshal(value); err != nil {
				return fmt.Sprint(value)
			} else {
				return string(jsonContent)
			}
		}
	}
}

// Type assert api for String().
type apiString interface {
	String() string
}

// Type assert api for Error().
type apiError interface {
	Error() string
}

/***
首字母大写
2024-11-08 09:49
wangwei
*/
func Capitalize(path string) string {
	if path == "" {
		return ""
	}
	return strings.ToUpper(path[:1]) + path[1:]
}

/****
首字母小写
2024-12-04 09:29
wangwei
 */
func LowercaseFirstLetter(s string) {
	if s == "" {
		return
	}
	runes := []rune(s)
	// 判断首字母是否是大写字母，如果是则转换为小写字母
	if unicode.IsUpper(runes[0]) {
		runes[0] = unicode.ToLower(runes[0])
	}
	result := string(runes)
	fmt.Println(result)
}


/****
 * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空，则返回空字符串。 例如：HELLO_WORLD->HelloWorld

 */

func ConvertToCamelCase(s string) string {
	if s == "" {
		return s
	}

	words := strings.Split(s, "_")
	var camel string

	for i, word := range words {
		if i == 0 {
			camel = word
		} else {
			camel += strings.Title(word)
		}
	}

	return camel
}
